package com.tencent.hummingbird.portal.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Collection;

@RestController
@Slf4j
public class HomeController {
    private static final GrantedAuthority STUDENT = new SimpleGrantedAuthority("ROLE_STUDENT");
    private static final GrantedAuthority TEACHER = new SimpleGrantedAuthority("ROLE_TEACHER");
    @GetMapping("/index.html")
    private ModelAndView index(@AuthenticationPrincipal UserDetails user){
         Collection<? extends GrantedAuthority> authorities = user.getAuthorities();
         if(authorities.contains(STUDENT)){
             return new ModelAndView("index");
         }else if(authorities.contains(TEACHER)){
             return new ModelAndView("index_teacher");
         }
        return null;
    }
    @GetMapping("/question/create.html")
    public ModelAndView creatQuestion(){
        return new ModelAndView("question/create");
    }
    @GetMapping("/question/detail.html")
    public ModelAndView detail(){
        return new ModelAndView("question/detail");
    }
}
